package com.bilin.coin.list;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bilin.coin.ApiConstant;
import com.bilin.coin.R;
import com.bilin.coin.bean.MillResourceBean;
import com.bilin.coin.bean.MyMillBean;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.tools.Poster;
import com.bilin.coin.utils.DateUtil;
import com.bilin.coin.utils.DensityUtils;
import com.bilin.coin.utils.GlideUtils;
import com.bilin.coin.utils.GsonUtil;
import com.bilin.coin.utils.SpacesItemDecoration;
import com.bilin.coin.widget.AbsPullToRefreshRecycleView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MyMillList extends AbsPullToRefreshRecycleView<MyMillBean.RecordsBean> {


    private int type;

    public MyMillList(BaseAppCompatActivity activity, int type) {
        super(activity, true, false, true);
        this.type = type;
    }

    @Override
    public int fillPageSize() {
        return 100;
    }

    @Override
    public boolean isPost() {
        return false;
    }

    @Override
    public List parseListDataAndFillTotal(String dataJson) {
        MyMillBean memberBean = GsonUtil.GsonToBean(dataJson, MyMillBean.class);
        totalCount = memberBean.getTotal();
        return memberBean.getRecords();
    }

    @Override
    public Map<String, String> fillParams() {
        HashMap params = new HashMap();
        params.put("currentPage", page + "");
        params.put("pageSize", pageSize + "");
        params.put("type", type + "");
        return params;
    }

    @Override
    protected RecyclerView.LayoutManager fillLayoutManger() {
        LinearLayoutManager manager = new LinearLayoutManager(activity);
        manager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.addItemDecoration(new SpacesItemDecoration(1, DensityUtils.dp2px(activity, 10), 0x1888));
        return manager;
    }

    @Override
    public String fillUrl() {
        return "/api/bl-my-device/myDevice";
    }

    @Override
    protected void convertItem(BaseViewHolder helper, MyMillBean.RecordsBean item) {

        ImageView iv1 = helper.getView(R.id.iv1);
        Button bt_confirm = helper.getView(R.id.bt_confirm);
        try {
            //bt_confirm.setVisibility(item.isIsOverDue() ? View.GONE : View.VISIBLE);
            bt_confirm.setVisibility(!item.isCanAgain() ? View.GONE : View.VISIBLE);
            String imgUrl = item.getBlDevice().getImgUrl();
            if (imgUrl.startsWith("http")) {
                GlideUtils.getInstance().displayCurrencyImage(activity, imgUrl, iv1);
            } else {
                GlideUtils.getInstance().displayCurrencyImage(activity, ApiConstant.OSSURL + imgUrl, iv1);
            }
            helper.setText(R.id.tv_name, item.getBlDevice().getName());



            //helper.setText(R.id.tv_gold, "金豆总奖励:" + item.getBlDevice().getAwardJd());
            helper.setText(R.id.tv_gold, "金豆总奖励：" + item.getTotalAward());
            //helper.setText(R.id.tv_work_time, "工作时间:" + DateUtil.getDiffDDHHMMSS2(Long.parseLong(item.getBlDevice().getWorkTime())*1000+""));
            helper.setText(R.id.tv_work_time, "工作时间：" + item.getWorkTimeStr());

            //helper.setText(R.id.tv_valid_time, "有效期：" + item.getValidTime());
            helper.setText(R.id.tv_valid_time, "" + item.getValidTime());


            bt_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    xuqi(item.getId());
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void xuqi(String id) {
        new Poster(activity, true, true) {
            @Override
            protected String fillUrl() {
                return "/api/bl-my-device/again";
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap hashMap = new HashMap();
                hashMap.put("id", id + "");
                return hashMap;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                refresh(true);
            }
        };
    }

    @Override
    protected int fillAnimationType() {
        return BaseQuickAdapter.ALPHAIN;
    }

    @Override
    protected int fillItemLayout() {
        return R.layout.item_my_mill;
    }
}
